package com.neutech.view;

import com.neutech.dao.BusinessDAO;
import com.neutech.dao.FoodDAO;
import com.neutech.dao.impl.BusinessDAOlmpl;
import com.neutech.dao.impl.FoodDAOlmpl;
import com.neutech.entity.Business;
import com.neutech.entity.Food;

import java.math.BigDecimal;
import java.util.List;
import java.util.Scanner;
import static com.neutech.constant.Constant.input;

public class BusinessView {
//    private static Scanner input = new Scanner(System.in);

    //多态，通过BusinessDAO,来调用实现类BusinessDAOlmpl
    private static BusinessDAO businessDAO = new BusinessDAOlmpl();

    private static FoodDAO foodDAO = new FoodDAOlmpl();

    //管理员的管理界面
    public static void showMenu(){
        System.out.println("1.查看所有商家的信息");
        System.out.println("2.增加商家");
        System.out.println("3.修改商家");
        System.out.println("4.删除商家");
        System.out.println("5.查看所有食品信息");
        System.out.println("6.增加食品信息");
        System.out.println("7.修改食品信息");
        System.out.println("8.删除食品");
        System.out.println("9.回到上级菜单");
        System.out.println("输入编号选择功能");
        int index = input.nextInt();
        if(index == 1){
          showBusiness();
        } else if (index == 2 ) {
           addBusiness();
        } else if (index == 3) {

        } else if (index ==4) {
            deleteBusiness();
        } else if (index == 5) {
            showFood();
        } else if (index == 6) {
            addFood();
        } else if (index == 7) {

        }else if (index == 8){

        } else if (index == 9) {
            //z直接返回到登入界面
            AdminView.showMenu();
        } else {
            System.out.println("没有此功能");
            showMenu();
        }

        }


                    //方法1.查看所有是商家
        public static void showBusiness (){

            List<Business> businessList = businessDAO.selectAll();
            if(businessList.size() == 0){
                System.err.println("没有商家数据");
            }else {
                for (Business business : businessList) {
                    System.out.println(business);
                }
            }
            showMenu();
        }

                 //方法2.增加商家
        public static void addBusiness(){
          //1.录入信息.在录入前判断商家是否已经存在
            System.out.println("请输入商家名");
            String businessName = input.next();
          //2.判断商家名是否重复
          if(businessDAO.selectBusinessName(businessName).getBusinessId() == null){
              //未查询到重复。输入添加的值
              System.out.println("请输入密码");
              String password = input.next();
              System.out.println("请输入商家地址");
              String businessAddress = input.next();
              System.out.println("请输入商家描述");
              String businessExplain = input.next();
              System.out.println("请输入起送费");
              BigDecimal startPrice = input.nextBigDecimal();
              System.out.println("请输入配送费");
              BigDecimal deliceryPrice = input.nextBigDecimal();

              // 执行增加逻辑
              Business business = new Business();
              business.setPassword(password);
              business.setBusinessName(businessName);
              business.setBusinessAddress(businessAddress);
              business.setBusinessExplain(businessExplain);
              business.setStartPrice(startPrice);
              business.setDeliveryPrice(deliceryPrice);

              businessDAO.add(business);
              System.out.println("增加成功");

          }else {
              System.err.println("商家名重复");
          }
          //程序最后继续跳转到管理员界面
          showMenu();
        }

       //方法4.删除商家
      public static void deleteBusiness(){
        //1.判断商家是否存在
          System.out.println("请输入商家名");
          String businessName2 = input.next();
        //2.如果不存在提示，商家不存在。存在执行删除
          if(businessDAO.selectBusinessName(businessName2).getBusinessId() != null){
              Business business = new Business();
              business.setBusinessName(businessName2);
              businessDAO.delete(business);
              System.out.println("删除成功");
          }else {
              System.err.println("未查询到商家");
          }
          showMenu();
      }


      //方法5.称查询所有食品
      public static void showFood(){
        //使用集合接收查询的所有结果
        List<Food> foodList = foodDAO.selectAllFood();
        //判断是否查询到商家
          if( foodList.size() == 0){
              System.out.println("未查询到食品");
          }else {
         //遍历集合，展示食品信息*-
              for (Food food : foodList) {
                  System.out.println(foodList);
              }
          }
        showMenu();
      }

      //方法6.实现增加食品信息
    public static void addFood(){
        System.out.println("请输入食品名");
        String foodName = input.next();
        //首先通过食品名判断是否存在
        Food food = new Food();
        //判断：对象foodDAO 中的selectFoodName 方法 查询出来的foodId是否为空
      if(foodDAO.selectFoodName(foodName).getFoodId() != null){
          //食品名存在
          System.out.println("食品存在");

      } else {
          //食品名不存在执行增加方法
          System.out.println("请输入食品名");
          String foodName2 = input.next();
          System.out.println("请输入食品信息");
          String foodExplain = input.next();
          System.out.println("请输入食品价格");
          BigDecimal foodPrice = input.nextBigDecimal();
          System.out.println("请输入商家编号");
          int businessId = input.nextInt();

          //将录入的值，添加到food对象中
          //set方法中的接收值为控制台录入的值
          food.setFoodName(foodName2);
          food.setFoodExplain(foodExplain);
          food.setPrice(foodPrice);
          food.setBusinessId(businessId);
          foodDAO.add(food);
          System.out.println("添加成功");
      }
     showMenu();
    }
}


